package main;

import aux.GUIPro;
import aux.NumberPro;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

/**
 *
 * @author yan
 */
public class WelcomeFrame extends JFrame{
    Box b;
    JButton easy,medium,hard,impossible,random,custom,challenges,scores,options;
    Border bor;
    Difficulty d;
    Eventer evt;
    boolean difficultySet;

    public WelcomeFrame() {
        initialize();
    }

    private void initialize() {
        difficultySet = false;
        evt = new Eventer();
        bor = BorderFactory.createEtchedBorder(EtchedBorder.RAISED);
        bor = BorderFactory.createTitledBorder(bor, "Difficulty", TitledBorder.CENTER,TitledBorder.TOP);

        b = Box.createVerticalBox();
        b.setBorder(bor);

        easy = new JButton("Easy");
        easy.setMnemonic('E');
        easy.addActionListener(evt);

        medium = new JButton("Medium");
        medium.setMnemonic('M');
        medium.addActionListener(evt);

        hard = new JButton("Hard");
        hard.setMnemonic('H');
        hard.addActionListener(evt);

        impossible = new JButton("Impossible");
        impossible.setMnemonic('I');
        impossible.addActionListener(evt);
        
        random = new JButton("Random");
        random.setMnemonic('R');
        random.addActionListener(evt);

        custom = new JButton("Custom");
        custom.setMnemonic('C');
        custom.addActionListener(evt);
        
        challenges = new JButton("Challenges");
        challenges.setMnemonic('A');
        challenges.addActionListener(evt);

        scores = new JButton("Scores");
        scores.setMnemonic('S');
        scores.addActionListener(evt);

        options = new JButton("Options");
        options.setMnemonic('O');
        options.addActionListener(evt);

        this.setTitle(null);
        this.setSize(200, 400);
        this.setResizable(false);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        b.add(Box.createGlue());
        GUIPro.HCenter(b,easy);
        b.add(Box.createVerticalStrut(10));
        GUIPro.HCenter(b,medium);
        b.add(Box.createVerticalStrut(10));
        GUIPro.HCenter(b,hard);
        b.add(Box.createVerticalStrut(10));
        GUIPro.HCenter(b,impossible);
        b.add(Box.createVerticalStrut(10));
        GUIPro.HCenter(b,random);
        b.add(Box.createVerticalStrut(10));
        GUIPro.HCenter(b,custom);
        b.add(Box.createVerticalStrut(10));
        GUIPro.HCenter(b,challenges);
        b.add(Box.createVerticalStrut(10));
        GUIPro.HCenter(b,scores);
        b.add(Box.createVerticalStrut(10));
        GUIPro.HCenter(b,options);
        b.add(Box.createGlue());

        this.add(b);
        this.pack();
    }

    public void close () {
        System.exit(0);
    }

    public synchronized void doStuff () {
        this.setVisible(true);
        while (!difficultySet) {
            try {
                this.wait(100);
            } catch (InterruptedException ex) {}
        }
        return;
    }

    private class Eventer implements ActionListener{

        public void actionPerformed(ActionEvent e) {
            if(e.getSource()==options) {
                OptionsFrame opf = new OptionsFrame();
            }
            else if(e.getSource()==custom) {
                CustomFrame cf = new CustomFrame();
            }
            else if(e.getSource()==scores) {
                ScoresFrame sf = new ScoresFrame();
            }
            else if(e.getSource()==random) {
                d = Difficulty.values()[NumberPro.random(0,4)];
                difficultySet = true;
            }
            else if (e.getSource()==challenges) {
                ChallengesFrame cf = new ChallengesFrame();
            }
            else {
                JButton aux = (JButton) e.getSource();
                d = Difficulty.valueOf(aux.getText().toUpperCase());
                difficultySet = true;
            }
        }

    }
}